package com.jb.repair.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.jb.repair.R;

/**
 * Created by wp1988 on 2016/7/18 0018.
 */
public class ItemTitleView extends RelativeLayout implements OnClickListener {

    private TextView tvTitle;
    private TextView tvRightText;
    private Button tvRightBtn;
    private OnItemTitleViewClickListener onItemTitleViewClickListener;

    private static final String TAG = "ItemTitleView";

    public ItemTitleView(Context context){
        super(context);
    }


    public ItemTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ItemTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context,  AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.layout_item_title, this, true);
        tvTitle = (TextView) findViewById(R.id.tv_item_title);
        tvRightText = (TextView)findViewById(R.id.tv_right_text);
        tvRightBtn = (Button)findViewById(R.id.btn_right);
        tvRightText.setOnClickListener(this);
        tvRightBtn.setOnClickListener(this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemTitleView);
        tvTitle.setText(a.getString(R.styleable.ItemTitleView_itemTitleleftText));
        int type = a.getInteger(R.styleable.ItemTitleView_type, 0);
        switch(type) {
            case 0:// 右边为文本
                tvRightBtn.setVisibility(GONE);
                tvRightText.setText(a.getString(R.styleable.ItemTitleView_itemTitleRightText));
                tvRightText.setTextColor(a.getColor(R.styleable.ItemTitleView_itemTitleRightTextColor, getResources().getColor(R.color.gray_dark)));
                break;
            case 1:// 右边为button
                tvRightText.setVisibility(GONE);
                tvRightBtn.setText(a.getString(R.styleable.ItemTitleView_itemTitleRightText));
                break;
        }
//        Drawable icon = ContextCompat.getDrawable(context, a.getResourceId(R.styleable.ItemTitleView_itemTitleRightIcon, 0));
//        tvRightText.setCompoundDrawables(icon, null, null, null);
    }

    public void setTvLeftText(String text) {
        tvTitle.setText(text);
    }

    public void setTvRightText(String text){
        tvRightText.setText(text);
    }


    public void setOnClickListener(OnItemTitleViewClickListener onItemTitleViewClickListener) {
        this.onItemTitleViewClickListener = onItemTitleViewClickListener;
    }


    @Override
    public void onClick(View view) {
        if (onItemTitleViewClickListener == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_right_text:
                onItemTitleViewClickListener.onRightTextClick(this.tvRightText);
                break;
            case R.id.btn_right:
                onItemTitleViewClickListener.onRightBtnClick(this.tvRightBtn);
                break;
            case R.id.tv_item_title:
                onItemTitleViewClickListener.onLeftTextClick(this.tvTitle);
                break;
        }
    }

    /**
     * 标题控件点击监听器
     */
    public static class OnItemTitleViewClickListener {
        /**
         * 点击右侧文本触发
         *
         * @param view
         */
        public void onRightTextClick(View view) {
        }

        /**
         * 点击右侧按钮触发
         *
         * @param view
         */
        public void onRightBtnClick(View view) {

        }

        public void onLeftTextClick(View view) {

        }
    }
}
